package org.cfx.gift.service;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.PolicyConditions;
import org.cfx.gift.config.AliOssConfig;
import org.cfx.gift.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.UUID;
/**
 * @author RTX 9090
 */
@Service
public class FileService {
    //读取阿里云oss配置
    @Resource
    private AliOssConfig aliOssConfig;

    public Map<String, String> getSign(){
        // Endpoint以成都为例，其它Region请按实际情况填写。
        String endpoint = aliOssConfig.getEndpoint();
        // 填写Bucket名称，例如examplebucket。
        String bucket = aliOssConfig.getBucketName();
        // 填写Host名称，格式为https://bucketname.endpoint。
        String host = "https://"+bucket+"."+aliOssConfig.getEndpoint();
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build("https://"+endpoint, aliOssConfig.getAccessKey(), aliOssConfig.getSecretKey());
        try {
            long expireTime = 30;
            long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
            Date expiration = new Date(expireEndTime);
            // PostObject请求最大可支持的文件大小为5 GB，即CONTENT_LENGTH_RANGE为5*1024*1024*1024。
            PolicyConditions policyConds = new PolicyConditions();
            policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
            // 设置上传到OSS文件的前缀，可置空此项。置空后，文件将上传至Bucket的根目录下。
            String dir = "images/";
            policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);

            String postPolicy = ossClient.generatePostPolicy(expiration, policyConds);
            byte[] binaryData = postPolicy.getBytes("utf-8");
            String accessId = aliOssConfig.getAccessKey();
            String encodedPolicy = BinaryUtil.toBase64String(binaryData);
            String postSignature = ossClient.calculatePostSignature(postPolicy);

            Map<String, String> respMap = new LinkedHashMap<String, String>();
            respMap.put("accessid", accessId);
            respMap.put("policy", encodedPolicy);
            respMap.put("signature", postSignature);
            respMap.put("dir", dir);
            respMap.put("host", host);
            respMap.put("expire", String.valueOf(expireEndTime / 1000));

            return respMap;
        } catch (Exception e) {
            // Assert.fail(e.getMessage());
            System.out.println(e.getMessage());
            return null;
        } finally {
            ossClient.shutdown();
        }
    }

    public Boolean  delete(String objectNameUrl){
        // Endpoint以成都为例，其它Region请按实际情况填写。
        String endpoint = aliOssConfig.getEndpoint();
        // 填写Bucket名称，例如examplebucket。
        String bucket = aliOssConfig.getBucketName();
        System.out.println(objectNameUrl);
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build("https://"+endpoint, aliOssConfig.getAccessKey(), aliOssConfig.getSecretKey());
        System.out.println(ossClient);
        if (!ossClient.doesObjectExist(bucket, objectNameUrl)) {
            return false;
        }
        ossClient.deleteObject(bucket, objectNameUrl);
        return true;
    }

}